package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

public class CourseContentServiceImpl implements CourseContentService {

    CourseContentDao contentDao = new CourseContentDaoImpl();

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        List<Course_Section> section = contentDao.findSectionAndLessonByCourseId(courseId);
        return section;
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        Course course = contentDao.findCourseByCourseId(courseId);
        return course;
    }

    @Override
    public String saveSection(Course_Section section) {

        section.setStatus(2);
        String dateFormat = DateUtils.getDateFormat();
        section.setCreate_time(dateFormat);
        section.setUpdate_time(dateFormat);
        int row = contentDao.saveSection(section);

        String result = null;
        if (row > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }

        return result;
    }

    @Override
    public String updateSection(Course_Section section) {

        section.setUpdate_time(DateUtils.getDateFormat());

        int row = contentDao.updateSection(section);

        String result = null;
        if (row > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }
        return result;
    }

    @Override
    public String updateSectionStatus(int id, int status) {

        int row = contentDao.updateSectionStatus(id, status);
        String result = null;
        if (row > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }

        return result;
    }

    @Override
    public String saveLesson(Course_Lesson lesson) {

        int row = contentDao.saveLesson(lesson);
        String result = null;
        if (row > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }
        return result;
    }

    @Override
    public String updateLesson(Course_Lesson lesson) {

        int update = contentDao.updateLesson(lesson);
        String result = null;
        if (update > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }
        return result;
    }
}
